package com.atguigu.utils;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Calendar;
import java.util.Random;

public class ClickSource implements SourceFunction<ClickEvent> {
    private String[] userArray = {"Mary", "Bob", "Alice", "John", "Liz"};
    private String[] urlArray = {"./home", "./cart", "./fav", "./prod?id=1"};
    private Random random = new Random();

    private boolean running = true;

    @Override
    public void run(SourceContext<ClickEvent> ctx) throws Exception {
        while (running) {
            ctx.collect(
                    new ClickEvent(
                            userArray[random.nextInt(userArray.length)],
                            urlArray[random.nextInt(urlArray.length)],
                            Calendar.getInstance().getTimeInMillis()
                    )
            );
            Thread.sleep(100L);
        }
    }

    // flink cancel jobid
    @Override
    public void cancel() {
        running = false;
    }
}
